<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests;
use Code;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;
use Session;
// require_once '../resources/views/admin/login/Code.class.php';
class LoginController extends Controller
{
    //显示登录页面

    public function getCode(){
       $code=new Code;
       $code->make();
       
    }
   static public function getXianshi(){
        $code=new Code;
       return $code->get();
    }
    public function getIndex(){
        return view('/admin/login/login');
    }
    public function postLogin(Request $request){
        // dd($request->except('_token'));
        $data=array();
        $data=$request->except('_token');
        // $data['a_upassword']=Hash::make($data['a_upassword']);
        //dd($data);
        $info=DB::table('admin_user')->where('a_uname','=',$data['a_uname'])->first();
        // dd($info);
        //验证用户名是否正确
        if(empty($info['a_uname'])){
            $request->flashExcept('a_uname');
            return back()->with('uerror','用户名错误');
        }
        //验证密码是否正确
        if(!(Hash::check($data['a_upassword'],$info['a_upassword']))) {
            $request->flashExcept('a_upassword');
            return back()->with('perror','密码错误');
        }
        //验证码是否正确
        
        //将验证码小写
        $code=strtolower(session('code'));

        // dd($code);
        $data['code']=strtolower($data['code']);

        if(!($data['code']==$code)){
            $request->flashExcept('code');
            return back()->with('cerror','验证码输入错误');
        }
    
        session(['data'=>$info]);
        return redirect("/admin/goods/index?a_uid=".$info['a_uid']);
    }

    //修改密码页面
    public function getPassedit(Request $request){
        //获得参数中的id便于查询
        $a_uid=$request->input('a_uid');
        //通过id查询相关信息
        $data=DB::table('admin_user')->where('a_uid','=',$a_uid)->first();
        //返回模板 并且发送数据
        return view('/admin/login/passedit')->with('data',$data);
    }
    //进行修改密码
    public function postPassupdate(Request $request){
        //接收表单和参数中的数据
        $pwd=$request->only('oldpassword','a_uid','a_upassword');
        //用得到的id查询相关信息
        $info=DB::table('admin_user')->where('a_uid','=',$pwd['a_uid'])->first();
        //验证密码是否正确 使用哈希验证
        if(!(Hash::check($pwd['oldpassword'],$info['a_upassword']))) {
            //如果错误就返回并且抛出错误
            return back()->with('error','原来密码错误');
        }
        //进行修改验证
        $this->validate($request, [
            'oldpassword' => 'required',
            'a_upassword' => 'required',
            'repassword' => 'required|same:a_upassword',
        ],[
            'oldpassword.required'=>'原密码必填',
            'a_upassword.required'=>'新密码必填',
            'repassword.required'=>'确认密码必填',
            'repassword.same'=>'密码不一致',
        ]);
        //通过验证后把表单中的密码进行哈希加密
        $info['a_upassword']=Hash::make($pwd['a_upassword']);
        $pwd=$info['a_upassword'];
        //修改密码操作
        $res=DB::table('admin_user')->where('a_uid','=',$info['a_uid'])->update(['a_upassword'=>"$pwd"]);
        if($res>0){
            return back()->with('success','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
    }
    //上传头像页面
    public function getPic(Request $request){
        $a_uid=$request->input('a_uid');
        $data=DB::table('admin_user')->where('a_uid','=',$a_uid)->first();
        // dd($data);
        session(['data'=>$data]);
        return view('/admin/admin_user/pic',['data'=>$data]);
    }
    //处理自定义上传图片
    public function postUpload(Request $request){
        //检测是否有文件上传
        $a_uid=$request->input('a_uid');
        // dd($a_uid);
        if($request->hasFile('a_upic')){
            //获取文件后缀
            $hz=$request->file('a_upic')->getClientOriginalExtension();
            //文件名字
            $temp_name=md5(time()+rand(1111,9999));
            $name=$temp_name.'.'.$hz;
            $res=DB::table('admin_user')->where('a_uid','=',$a_uid)->update(['a_upic'=>$name]);
            //将文件移动到新的位置
            
            if($res>0){
                $request->file('a_upic')->move('./upload/user',$name);
            }
            $data=DB::table('admin_user')->where('a_uid','=',$a_uid)->first();
            session(['data'=>$data]);
             return redirect("/login/pic?a_uid=$a_uid")->with('success','上传成功');
        }else{
            return back()->with('error','上传失败');
        }

        // dd($request->all());
    }
    //处理固定图片
    public function getFpic(Request $request){
        $a_uid=$request->input('a_uid');
        $name=$request->input('pic');
        // dd($name);
        $res=DB::table('admin_user')->where('a_uid','=',$a_uid)->update(['a_upic'=>$name]);
        $data=DB::table('admin_user')->where('a_uid','=',$a_uid)->first();
        session(['data'=>$data]);
        if($res>0){
           return redirect("/login/pic?a_uid=$a_uid")->with('success','修改成功');
        }
             
    }
    //用户退出
    public function getOut(){
        //删除session
        session()->pull('data');
        return redirect('/login/index');
    }
}
